package pchi.pchicamera;

import java.io.ByteArrayOutputStream;

import pchi.pchicamera.data.SizeDevices;
import pchi.pchicamera.functions.ImageEditting;
import pchi.pchicamera.functions.OnTouchImage;
import pchi.pchicamera.helpers.BitmapHelpers;
import pchi.pchicamera.helpers.LayoutHelpers;
import pchi.pchicamera.models.PhotoModel;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class CompositeActivity extends Activity implements OnClickListener {

	private String strImagePathResourceOne;
	private Bitmap resourceOne;

	private String strImagePathResourceTwo;
	private Bitmap resourceTwo;

	private ImageView imgComposite;

	private int ResquestWIDTH;
	private int ResquestHEIGHT;

	private ImageButton btnComposite;

	private Canvas canvas;
	private Paint paint;

	private ImageButton btnSave;

	private ImageButton btnEdit;

	private Bitmap originalBitmapTwo;
	private Bitmap originalBitmapOne;

	private Uri uriImageGotoComposite = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.composite_activity);

		imgComposite = (ImageView) findViewById(R.id.imgCompositeImages);

		btnComposite = (ImageButton) findViewById(R.id.btn_composite_select_image);
		btnComposite.setOnClickListener(this);

		Intent i = getIntent();
		Bundle bundle = i.getBundleExtra("imgComposite");
		strImagePathResourceOne = bundle.getString("imgPathComposite");

		originalBitmapOne = BitmapHelpers.decodeFile(strImagePathResourceOne,
				LayoutHelpers.getDeviceWidth(getApplicationContext()), 0);
		ResquestWIDTH = SizeDevices.getWidth(CompositeActivity.this);
		ResquestHEIGHT = SizeDevices.getHeight(CompositeActivity.this);

		// scale x,y
		// image root
		originalBitmapOne = Bitmap.createScaledBitmap(originalBitmapOne,
				ResquestWIDTH, ResquestHEIGHT, false);

		// image copy and use edit
		resourceOne = originalBitmapOne.copy(Bitmap.Config.RGB_565, true);
		imgComposite.setImageBitmap(resourceOne);

		btnSave = (ImageButton) findViewById(R.id.btn_composite_save);
		btnSave.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_composite_select_image:
			Intent i = new Intent(CompositeActivity.this, AlbumActivity.class);
			startActivityForResult(i, 2);
			break;
		default:
		case R.id.btn_composite_save:
			uriImageGotoComposite = OnTouchImage.saveImage(
					getApplicationContext(), resourceTwo);
			break;

		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 2) {
			strImagePathResourceTwo = data.getStringExtra("MESSAGE");
			originalBitmapTwo = BitmapHelpers.decodeFile(
					strImagePathResourceTwo,
					LayoutHelpers.getDeviceWidth(getApplicationContext()), 0);
			originalBitmapTwo = Bitmap.createScaledBitmap(originalBitmapTwo,
					originalBitmapTwo.getWidth(),
					originalBitmapTwo.getHeight(), false);
			resourceTwo = originalBitmapTwo.copy(Bitmap.Config.RGB_565, true);

			canvas = new Canvas(resourceTwo);
			paint = new Paint();
			canvas.drawBitmap(resourceOne, 0, 0, paint);
			paint.setXfermode(new PorterDuffXfermode(
					android.graphics.PorterDuff.Mode.MULTIPLY));
			canvas.drawBitmap(resourceTwo, 0, 0, paint);

			imgComposite.setImageBitmap(resourceTwo);
		}
	}

}
